//首页模块
import {
    RECEIVE_ADDRESS,
    RECEIVE_CATEGORYS,
    RECEIVE_SHOPS,
} from '../mutation-type'
import {
    reqAddress,
    reqCategorys,
    reqShops,
} from '@/api';
export default {
    state:{
        // 113.84435,22.631795
        latitude:22.631795,//纬度
        longitude:113.84435,//经度
        address:{},//地址对象
        categorys:[],//分类数组
        shops:[],//商家信息,
     },
    mutations: {
        [RECEIVE_ADDRESS](state,address){
            state.address=address;
        },
        [ RECEIVE_CATEGORYS](state,categorys){
            state.categorys=categorys;
        },
        [RECEIVE_SHOPS](state,shops){
            state.shops=shops;
        },
     },
    actions: {  
         //获取当前地址信息对象的异步action
    async getAddress({commit,state}){
        const {longitude,latitude}=state
        //发送异步请求
        const result=await reqAddress(longitude,latitude)
        //请求成功，提交mutation
        if(result.code===0){
            const address=result.data;
            commit(RECEIVE_ADDRESS,address)
        }
    },
     //获取商品分类数组的异步action
     async getCategorys({commit}){
        //发送异步请求
        const result=await reqCategorys()
        //请求成功，提交mutation
        if(result.code===0){
            const categorys=result.data;
            commit(RECEIVE_CATEGORYS,categorys)
        }
    },
     //获取商家列表数组的异步action
     async getShops({commit,state}){
        const {longitude,latitude}=state
        //发送异步请求
        const result=await reqShops({longitude,latitude})
        //请求成功，提交mutation
        if(result.code===0){
            const shops=result.data;
            commit(RECEIVE_SHOPS,shops)
        }
    },
    },
    getters: { }
}